package me.lwn.auth.resources;

import me.lwn.auth.application.LockedRecordManager;
import me.lwn.auth.common.HttpMsg;
import me.lwn.auth.common.PageRequest;
import me.lwn.auth.common.PageResponse;
import me.lwn.auth.domain.LockedRecord;
import me.lwn.auth.security.utils.LocalMessageSource;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("locked-record")
public class LockedRecordController {

    private final LockedRecordManager manager;
    private final MessageSourceAccessor message = LocalMessageSource.getAccessor();

    public LockedRecordController(LockedRecordManager manager) {
        this.manager = manager;
    }

    /**
     * 用户锁定记录分页列表
     */
    @PostMapping("page")
    public PageResponse<LockedRecord> page(@RequestBody PageRequest params) {
        return manager.findPage(params);
    }

    /**
     * 解锁用户接口
     *
     * @param id 记录id
     */
    @PostMapping("release/{id}")
    public HttpMsg<String> release(@PathVariable String id) {
        manager.release(id);
        return HttpMsg.success(message.getMessage("LockedRecordController.releaseSuccess"));
    }

    /**
     * 全部解锁
     */
    @PostMapping("release-all")
    public HttpMsg<String> releaseAll() {
        manager.releaseAll();
        return HttpMsg.success(message.getMessage("LockedRecordController.releaseAllSuccess"));
    }
}